<?php

namespace App\Http\Controllers\Api;

use App\Interface\CurdController;
use App\Librarys\Message;
use App\Models\ToolsPasswordModel;
use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class ToolsPasswordController extends BaseController implements CurdController
{
    protected Request $request;
    protected Model $toolsPasswordModel;

    public function __construct(Request $request, ToolsPasswordModel $toolsPasswordModel)
    {
        $this->request = $request;
        $this->toolsPasswordModel = $toolsPasswordModel;
    }

    public function create()
    {
        $flag = $this->request->flag;
        $password['status'] = $this->request->status;
        $password['type'] = $this->request->type;
        $password['app_name'] = $this->request->appName;
        $password['app_url'] = $this->request->appUrl;
        $password['email'] = $this->request->email;
        $password['account'] = $this->request->account;
        $password['password'] = $this->request->password;
        $password['remark'] = $this->request->remark;

        // 验证
        $validate = Validator::make($password, [
            'type' => 'required',
            'app_name' => 'required',
            'account' => 'required',
            'password' => 'required'
        ], [
            'type.required' => '密码分类不能为空',
            'app_name.required' => '应用名称不能为空',
            'account.required' => '账户名称不能为空',
            'password.required' => '登录密码不能为空'
        ]);
        // 判断
        if ($validate->fails()) { // 验证失败
            return Message::show(false, $validate->errors()->first());
        }

        switch ($flag) {
            default:
                return Message::show(false, '未定义操作');
                break;
            case 'create': // 新建密码信息
                // 验证通过,开启事务
                try {
                    // 写入数据库
                    $this->toolsPasswordModel->create($password);
                    // 提交事务
                    DB::commit();
                    return Message::show(true, '密码信息创建成功');
                } catch (Exception $error) {
                    // 事务回滚
                    DB::rollBack();
                    return Message::show(false, '密码信息创建失败：' . $error->getMessage());
                }

                break;
            case 'update': // 修改密码信息
                // 获取修改条件
                $id = $this->request->id;
                // 验证通过,开启事务
                try {
                    // 写入数据库
                    $this->toolsPasswordModel->where(['id' => $id])->update($password);
                    // 提交事务
                    DB::commit();
                    return Message::show(true, '密码信息修改成功');
                } catch (Exception $error) {
                    // 事务回滚
                    DB::rollBack();
                    return Message::show(false, '密码信息修改失败：' . $error->getMessage());
                }
                break;
        }
    }

    public function query()
    {
    }

    public function delete()
    {
    }
}
